<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/26
 * Time: 14:01
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\ExtendService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class ExtendController extends BaseController
{
    protected $service;

    public function __construct(Request $request, ExtendService $service)
    {
        parent::__construct($request);
        $this->service = $service;
    }

    /**
     * 推广的临时客户
     * @return \Illuminate\Http\JsonResponse
     */
    public function tempExtend()
    {
        $validator = Validator::make($this->request->post(), [
            'is_stay' => 'required|integer|in:0,1'
        ], [
            'is_stay.required'  =>  '是否现存不能为空',
            'is_stay.integer'   =>  '是否现存必须为整数',
            'is_stay.in'        =>  '是否现存选择错误'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_EXTEND_TEMP_ERROR, $validator->errors()->first());
        }

        $is_stay    =   $this->request->post('is_stay');
        $page       =   $this->request->input('page', 1);
        [$flag, $result] = $this->service->extendTemp((int)$is_stay, (int)$page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_EXTEND_TEMP_ERROR, $result);
        }
    }

    /**
     * 邀请人列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function inviteExtend()
    {
        $page   =   $this->request->input('page', 1);
        [$flag, $result] = $this->service->extendInvite((int) $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_EXTEND_INVITE_ERROR, $result);
        }
    }

    /**
     * 推广商品
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodExtend()
    {
        $validator = Validator::make($this->request->post(), [
            'name'     =>  'string|in:real_sales,commission_price,sell_price',
            'sorting'  =>  'string|in:desc,asc'
        ], [
            'name.string'   =>  '排序名称必须为字符',
            'name.in'       =>  '排序名称选择错误',
            'sorting.string'=>  '排序方式必须为字符',
            'sorting.in'    =>  '排序方式选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_EXTEND_GOOD_ERROR, $validator->errors()->first());
        }

        $sort    =   $this->request->post('name', 'p.id');
        $desc    =   $this->request->post('sorting', 'desc');
        $page    =   (int) $this->request->input('page', 1);
        [$flag, $result]    =   $this->service->extendGood($sort, $desc, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_EXTEND_GOOD_ERROR, $result);
        }
    }

    /**
     * 推广商品分享
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function shareExtend()
    {
        $validator = Validator::make($this->request->post(), [
            'unique_id'  =>  'required|string|exists:mini_product,unique_id',
        ], [
            'unique_id.required'    =>  '商品标识符不能为空',
            'unique_id.string'      =>  '商品标识符必须为字符',
            'unique_id.exists'      =>  '商品标识符不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_EXTEND_SHARE_ERROR, $validator->errors()->first());
        }

        $unique_id    =   $this->request->post('unique_id');
        [$flag, $result]    =   $this->service->extendShare($unique_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_EXTEND_SHARE_ERROR, $result);
        }
    }

}
